package MYARRAYLIST;

import java.util.Arrays;

public class MyArrayList implements List {
    public int[] array;
    public int usedSized=0;
    public static final int DEFAULT=10;
    public MyArrayList(){
        this.array=new int[DEFAULT];
    }
    @Override
    public void add(int date) {
        if(isFull()){
            grow();
        }
        this.array[this.usedSized]=date;
        usedSized++;
    }
    public boolean isFull(){
        return usedSized== array.length;
    }
    public void grow(){
        this.array= Arrays.copyOf(array,2*array.length);
    }

    public void checkPos(int pos) throws PosInlegal{
        if(pos<0||pos>usedSized){
            throw new PosInlegal("插入的位置不合法");
        }
    }
    @Override
    public void add(int pos, int date) {
        try{
            checkPos(pos);
            if(isFull()){
                grow();
            }
            //挪动元素；
            for(int i=usedSized-1;i<=pos;i--){
                array[i+1]=array[i];
            }
            array[pos]=date;
            usedSized++;
        }catch (PosInlegal e){
            System.out.println("插入的pos位置不合法");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contain(int tofind) {
        for(int i=0;i< array.length;i++){
            if(array[i]==tofind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int tofind) {
        for(int i=0;i< array.length;i++){
            if(array[i]==tofind){
                return i;
            }
        }
        return 0;
    }
public void checkEmpty() throws EmptyInlegal{
        if(usedSized==0){
            throw new EmptyInlegal("该顺序表为空");
        }
}
public void checkPos2(int pos) throws PosInlegal{
        if(pos<0||pos>=usedSized){
            throw new PosInlegal("要查找的位置不合法");
        }
}
    @Override
    public int get(int pos) {
        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch(EmptyInlegal e){
            System.out.println("顺序表为空异常");
            e.printStackTrace();
        }catch (PosInlegal e){
            System.out.println("插入位置不合法");
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos]=value;
        }catch (PosInlegal e){
            e.printStackTrace();
        }catch (EmptyInlegal e2){
            e2.printStackTrace();
        }
    }

    @Override
    public void remove(int remove) {
        try{
            checkEmpty();
            int pos=indexOf(remove);
            if(pos==-1){
                return;
            }
            for(int i=pos;i<usedSized-1;i++){
                array[i]=array[i+1];
            }
            usedSized--;
        }catch (EmptyInlegal E){
            E.printStackTrace();
        }
    }

    @Override
    public void disPlay() {
        for(int i=0;i< array.length;i++){
            System.out.print(array[i]+" ");
        }
    }
}
